﻿using System;

public partial class UserServer
{
    public User data = new User();
    public string uuid;
    public TCPClient client;
    public MatchInfo matchInfo;
    public BattleInfo battleInfo;
    public BattleResultInfo battleResultInfo;
    
    public double loginTime;
    public double lastOfflineTime;

    public void UpdateHomeAdTimesRemain(int times)
    {
        //更新数据库
        users.UpdateHomeAdTimesRemain(data.id, times);
    }

    public void AddDiamond(int addNum)
    {
        int old = data.diamond;
        int diamond = data.AddDiamond(addNum);
        GameDebug.Log($"用户：{data.id} 钻石由：{old} 变为{diamond}");
        OnUserUpdateDiamond(this, diamond);
    }
    public void MniusDiamond(int mniusNum)
    {
        int old = data.diamond;
        int diamond = data.MinusDiamond(mniusNum);
        GameDebug.Log($"用户：{data.id} 钻石由：{old} 变为{diamond}");
        OnUserUpdateDiamond(this, diamond);
    }

    void OnUserUpdateDiamond(UserServer userServer, int diamond)
    {
        //更新数据库
        users.UpdateDiamond(userServer.data.id, diamond);

        //发送事件
        NetEvent_UpdateDiamond updateDiamond = new NetEvent_UpdateDiamond();
        updateDiamond.diamondData = new UpdateDiamondData();
        updateDiamond.diamondData.diamond = diamond;

        updateDiamond.SendToRemote(userServer.client);
    }


    internal void UpdateFreeApplyCount(UserServer userServer, int value)
    {
        //更新数据库
        users.UpdateFreeApplyCount(userServer.data.id, value);
    }
    /// <summary>
    /// 改名
    /// </summary>
    /// <param name="nick"></param>
    public int ChangeName(string nick)
    { 
        if(!CheckNickValid(nick))
        {
            //昵称不合法
            return -2;
        }

       //更新数据库
        int row = users.UpdateNick(data.id, nick);

        if (row >= 0)
        {
            data.UpdateNick(nick);
            return 0;
        }
        else
        {
            return -1;
        }
        
    }

    bool CheckNickValid(string name)
    {
        //判断用户名合法性
        return true;
    }

    
    public void UpdateWinStage(int stage)
    {
        data.UpdateStageLevel(stage);

        users.UpdateWinStage(data.id, stage);
    }

    internal void ObtainLoginBonus(int day)
    {
        data.ObtainLoginBonus(day);

        users.UpdateObtainedLoginBonus(data.id, data.obtainLoginBonus);
    }

    //public void UnlockPet(int petId)
    //{
    //    UnlockPet(this, petId);
    //}

    //public void UnlockPet(UserServer userServer, int petId)
    //{
    //    PetArche petArche = ConstData.Instance.GetPet(petId);

    //    if (petArche == null)
    //    {
    //        GameDebug.LogError($"petArche == null petId:{petId}");
    //        return;
    //    }

    //    Pet userPet = userServer.data.FindPet(petId);

    //    if (userPet != null)
    //    {
    //        double curTime = CommonFunctions.GetTimeStampSeconds();

    //        int hp = ConstData.Instance.GetRankArche(1).hp;

    //        userPet.UpdateHp(hp, curTime);

    //        pets petTable = new pets(userServer.data.id, petId, hp, curTime,0, userPet.mood, userPet.usedSkinIndex, MysqlFunction.IntArray2String(userPet.propertyProportion.GetPropertiesArr()),userPet.continueLoseCount);

    //        GameDebug.Log($"unlock pet  user:{userServer.data.id} hp:{hp}  userPet:{userPet.lastUpdateHp}  userPet.hp:{userPet.hp} ");

    //        string database = users.GetUserDatabaseWithUserId(userServer.data.id);

    //        //更新数据库
    //        long newPetId = MysqlFunction.InsertIntoTable(database, SqlTable.pets, petTable);

    //        //更新数据
    //        userPet.UpdateId(newPetId);

    //        //发送事件
    //        NetEvent_UpdatePet netEvent_UpdatePet = new NetEvent_UpdatePet();
    //        netEvent_UpdatePet.updatePetData = new UpdatePetData();
    //        netEvent_UpdatePet.updatePetData.pet = userPet;

    //        netEvent_UpdatePet.SendToRemote(userServer.client);
    //    }
    //    else
    //    {
    //        GameDebug.LogError($"userPet == null  petId:{petId}");

    //        //int[] property = PropertyProportion.PropertyProportion2IntArr(petArche.attackProportion, petArche.defensiveProportion, petArche.speedProportion, petArche.jumpProportion);
    //        //Pet newPet = new Pet(exist.id, petArches[i], exist.used_skin_index, exist.power, propertyProportion, exist.hp_last_update, exist.hp_update_time, mood, maxHp, ConstData.Instance);
    //    }

    //}

    //public void UnlockSkin(int skinId)
    //{
    //    UnlockSkin(this,skinId);
    //}

    //public void UnlockSkin(UserServer userServer, int skinId)
    //{
    //    SkinArche skinArche = ConstData.Instance.GetSkin(skinId);

    //    if (skinArche == null)
    //    {
    //        GameDebug.LogError($"skinArche == null skinId:{skinId}");
    //        return;
    //    }

    //    Skin userSkin = userServer.data.GetSkin(skinId);

    //    if (userSkin != null)
    //    {
    //        double curTime = CommonFunctions.GetTimeStampSeconds();

    //        skins skinTable = new skins(userServer.data.id, skinId);

    //        string database = users.GetUserDatabaseWithUserId(userServer.data.id);

    //        //更新数据库
    //        long newSkinId = MysqlFunction.InsertIntoTable(database,SqlTable.skins, skinTable);

    //        //更新数据
    //        userSkin.UpdateId(newSkinId);

    //        //发送事件
    //        NetEvent_UpdateSkin netEvent_UpdatePet = new NetEvent_UpdateSkin();
    //        netEvent_UpdatePet.updateSkinData = new UpdateSkinData();
    //        netEvent_UpdatePet.updateSkinData.skin = userSkin;

    //        netEvent_UpdatePet.SendToRemote(userServer.client);
    //    }
    //    else
    //    {
    //        GameDebug.LogError($"userSkin == null  skinId:{skinId}");
    //    }

    //}

    //public int AddPower(int addPower)
    //{
    //    return AddPower(this, addPower);
    //}

    //int AddPower(UserServer userServer,int addPower)
    //{
    //    int newPower = addPower + userServer.data.power;

    //    if (newPower < 0) newPower = 0;

    //    userServer.data.UpdatePower(newPower);

    //    users.UpdatePower(userServer.data.id, newPower);

    //    return newPower;
    //}


    public override string ToString()
    {
        return $"userId:{data.id}  clientId:{this.client.clientId}";
    }

    //internal static texture[] FindInTextureTable(long userId)
    //{
    //    return MysqlFunction.FindRowsInTable<texture>(users.GetUserDatabaseWithUserId(userId), SqlTable.texture, "uid", userId.ToString());

    //}
    //public static texture FindTexInTable(long userId, int texId)
    //{
    //    return MysqlFunction.FindInTable<texture>(users.GetUserDatabaseWithUserId(userId), SqlTable.texture, "uid", userId.ToString(), "tex_id", texId.ToString());

    //}
}

public class MatchInfo
{
    public bool ready;
    public int matchMode;
   // public int teamId;
    public bool matchSuc;
}

public class BattleInfo
{
    public string ip;
    public int port;
    public int mapId;
    public string roomId;
}

public class BattleResultInfo
{
    //public BattleCoreResult battleResult;
    public bool adDouble;
}
